package pro.cherish.knowledgescattered.system;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 检查系统状态和打印系统信息
 */
@Component
public class SystemInfoIntroductionService {

    // private ISysconfLysService sysconfLysService;

    private static Formatter formatter=new Formatter(System.out);
    @Autowired
    private Environment environment;

    @PostConstruct
    private void init() {
        printHeader("cherishmall微信商城系统");
        Map<String, String> systemInfoMap = getSystemInfoMap();
        printInfo(systemInfoMap);
        printFooter("cherishmall微信商城系统");
    }

    private void printInfo(Map<String, String> systemInfoMap) {
        Set<Map.Entry<String, String>> entries = systemInfoMap.entrySet();
        entries.forEach(entry -> {
            printEntry(entry);
        });
    }

    private void printEntry(Map.Entry<String, String> info) {
        formatter.format("%-20s %-100s\n","[ "+info.getKey()+" ]",info.getValue());
    }

    private void printHeader(String header) {
        System.out.println("++++++++++++++++++++" + header + "++++++++++++++++++++");
    }

    private void printFooter(String footer) {
        System.out.println("++++++++++++++++++++" + footer + "++++++++++++++++++++");
    }

    private Map<String, String> getSystemInfoMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("数据库用户", environment.getProperty("spring.datasource.druid.username"));
        map.put("数据库地址", environment.getProperty("spring.datasource.druid.url"));
        map.put("微信调试级别", environment.getProperty("logging.level.com.cherish_lailai.cherishmall.wx"));
        map.put("首页显示记录数：NEW,HOT,BRAND,TOPIC,CatlogList,CatlogMore", SystemConfig.getNewLimit() + "," + SystemConfig.getHotLimit() + "," + SystemConfig.getBrandLimit() + ","+ SystemConfig.getTopicLimit() + "," + SystemConfig.getCatlogListLimit() + "," + SystemConfig.getCatlogMoreLimit());
        return map;
    }
}
